package com.danger.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.danger.activity.R;
import com.danger.model.entity.Song;
import com.danger.util.TimeUtil;

import java.util.List;

/**
 * Created by 俊 on 2016/8/15.
 */
public class SongAdapter extends BaseAdapter {

    private List<Song> songs;
    private LayoutInflater inflater;

    public SongAdapter(Context context, List<Song> songs) {
        inflater = LayoutInflater.from(context);
        this.songs = songs;
    }

    @Override
    public int getCount() {
        return songs.size();
    }

    @Override
    public Object getItem(int position) {
        return songs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listview_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Song song = songs.get(position);
        holder.item_song_name_textView.setText(song.getName());
        holder.item_artist_name_textView.setText(song.getArtist());
        holder.item_album_textView.setText(song.getAlbum());
        holder.item_duration_textView.setText(TimeUtil.parseDuration(song.getDuration()));
        return convertView;
    }

    private class ViewHolder {

        public TextView item_song_name_textView;
        public TextView item_artist_name_textView;
        public TextView item_album_textView;
        public TextView item_duration_textView;

        public ViewHolder(View view) {
            item_song_name_textView = (TextView) view.findViewById(R.id.item_song_name_textView);
            item_artist_name_textView = (TextView) view.findViewById(R.id.item_artist_name_textView);
            item_album_textView = (TextView) view.findViewById(R.id.item_album_textView);
            item_duration_textView = (TextView) view.findViewById(R.id.item_duration_textView);
        }
    }
}